<template>
  <!-- 汇总需求dilog -->
  <div>
    <el-dialog
      v-dialogDrag
      :title="info.title"
      :visible.sync="info.show"
      width="1400px"
      v-el-drag-dialog
      show-close="false"
      @close="dialogClose"
      @open="dialogShow"
      @opened="dialogOpened"
      destroy-on-close
      :close-on-click-modal="false"
    >
      <div class="basicInfoBox" v-if="info.topFormShow">
        <el-form ref="dialogFrom" :model="dialogFrom.data" :rules="dialogFrom.rules" label-width="150px" inline="true" size="mini">
          <el-row class="dialog-row">
            <el-col :span="8">
              <el-form-item label="标的编号：" prop="bidCode">
                <el-input clearable filterable :disabled="true" :value="dialogFrom.data.bidCode" style="width:200px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="标的名称：" prop="bidName">
                <el-input v-model="dialogFrom.data.bidName" placeholder="请输入标的名称" disabled clearable style="width:200px;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="运输总量(吨)：" prop="bidAmount">
                <el-input v-model="dialogFrom.data.bidAmount" placeholder="请输入数量" clearable disabled style="width:200px;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="dialog-row">
            <el-col :span="8">
              <el-form-item label="竞价方式：" prop="bidQuotations">
                <el-select v-model="dialogFrom.data.bidQuotations" :disabled="dialogFrom.disable.allDisable" clearable filterable style="width:200px;">
                  <el-option v-for="(item, index) in bidQuotations" :key="index" :label="item.rvCmeaning" :value="item.rvLowValue"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="平台合作：" prop="bidPlatformTogether">
                <el-select
                  v-model="dialogFrom.data.bidPlatformTogether"
                  clearable
                  filterable
                  :disabled="dialogFrom.disable.togtherDisable"
                  style="width:200px;"
                >
                  <el-option
                    v-for="(item, index) in [{ name: '是', code: 'Y' }, { name: '否', code: 'N' }]"
                    :key="index"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="dialog-row">
            <el-col :span="14">
              <el-form-item label="单价竞价范围(元)：" prop="bidOfferStart">
                <el-input-number
                  v-model="dialogFrom.data.bidOfferStart"
                  :disabled="dialogFrom.disable.allDisable"
                  :step="0.5"
                  :min="0.5"
                  :max="9999999"
                  :precision="1"
                  style="width:160px;"
                  @change="dialogInputNumberStartChange"
                ></el-input-number>
              </el-form-item>
              -
              <el-form-item prop="bidOfferEnd">
                <el-input-number
                  v-model="dialogFrom.data.bidOfferEnd"
                  :disabled="dialogFrom.disable.allDisable"
                  :step="0.5"
                  :min="0.5"
                  :max="9999999"
                  :precision="1"
                  style="width:160px;"
                  @change="dialogInputNumberEndChange"
                ></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="备注：" prop="bidRemark">
                <el-input
                  type="textarea"
                  v-model="dialogFrom.data.bidRemark"
                  style="width:400%"
                  :disabled="dialogFrom.disable.allDisable"
                  maxlength="200"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="tableTitle" v-if="info.topFormShow">需求信息</div>
      <div>
        <el-form class="from" :model="dTable.queryParam" label-width="100px">
          <el-row style="height:45px;">
            <el-col :span="6">
              <el-form-item label="需求编号:">
                <el-input placeholder="请输入编号" size="mini" style="width:180px;" v-model="dTable.queryParam.code"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="运输类型:">
                <el-select v-model="dTable.queryParam.tranType" placeholder="请选择" filterable clearable size="mini" style="width:180px;">
                  <el-option v-for="(item, index) in bidTypeDataList" :key="index" :label="item.rvCmeaning" :value="item.rvLowValue"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="运输方式:">
                <el-select v-model="dTable.queryParam.transportMode" placeholder="请选择" filterable clearable style="width:180px;" size="mini">
                  <el-option v-for="(item, index) in transportModeList" :key="index" :label="item.rvCmeaning" :value="item.rvLowValue"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="品种:">
                <el-select v-model="dTable.queryParam.kind" placeholder="请选择" filterable clearable style="width:180px;" size="mini">
                  <el-option v-for="(item, index) in kindModeList" :key="index" :label="item.varName" :value="item.varCode">
                    <span style="float: left">{{ item.varName }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.dptName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="height:45px;">
            <el-col :span="6" v-if="info.groupShow">
              <el-form-item label="集团:">
                <el-select v-model="dTable.queryParam.group" placeholder="请选择" filterable clearable size="mini" style="width:180px;">
                  <el-option v-for="(item, index) in groupDataList" :key="index" :label="item.dptName" :value="item.dptId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6" v-if="info.sugarModeListShow">
              <el-form-item :label="queryChildName">
                <el-select v-model="dTable.queryParam.dptId" placeholder="请选择" filterable clearable style="width:180px;" size="mini">
                  <el-option v-for="item in sugarModeList" :key="item.dptId" :label="item.dptName" :value="item.dptId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="submitStartTime" label="提交时间:">
                <el-date-picker
                  size="mini"
                  v-model="dTable.queryParam.submitStartTime"
                  type="datetime"
                  placeholder="请选择开始时间"
                  style="width:180px;"
                ></el-date-picker>
                —
                <el-date-picker
                  size="mini"
                  v-model="dTable.queryParam.submitEndTime"
                  type="datetime"
                  placeholder="请选择结束时间"
                  style="width:180px;"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="1">
              <el-form-item style="margin-left:0px">
                <el-button @click="initDemandTable" size="mini" type="primary">查询</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div>
        <el-table
          height="400"
          :data="dTable.data"
          v-loading="dTable.loading"
          @selection-change="demandSelectChange"
          ref="dTableRef"
          :row-key="dTableRowKey"
          @row-click="rowClick"
          id="scrollTable"
          border
        >
          <el-table-column align="center" label="序号" width="50" type="index"></el-table-column>
          <el-table-column align="center" v-if="info.selectBoxShow" width="45" type="selection" :selectable="isSelectable"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="demandCode" label="需求编号" width="150"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="brand" label="货物品牌"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="dailyOutput" label="规格"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="product" label="产品大类"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="kind" label="品种" width="150"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="transportType" label="运输类型"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="transportMode" label="运输方式"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="transportQuantity" label="运输数量(吨)" width="120"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="sfdSubmitTime" label="提交时间" width="170" v-if="info.submitTimeShow"></el-table-column>
          <el-table-column align="center" label="集团" prop="groupName" show-overflow-tooltip width="150"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="dptName" width="150" label="需求来源"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="remark" label="备注"></el-table-column>
          <el-table-column show-overflow-tooltip align="center" label="操作" width="300">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" plain @click="showTransportMethod(scope.row)">线路详情 </el-button>
              <el-button size="mini" v-show="info.historyShow" type="primary" plain @click="showHistory(scope.row)">驳回历史 </el-button>
              <el-button size="mini" type="primary" plain @click="refuse(scope.row)" v-if="info.refuseShow">驳回 </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="info.paginationShow">
        <el-pagination
          @size-change="dcs"
          @current-change="dci"
          :current-page.sync="dTable.pageInfo.pageIndex"
          :page-sizes="[10, 15]"
          :page-size="dTable.pageInfo.pageSize"
          layout="sizes, prev, pager, next, jumper, total"
          :total="dTable.pageInfo.total"
        ></el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.show = false" size="mini">{{ info.button.f }}</el-button>
        <el-button @click="confirm" size="mini" type="primary">{{ info.button.t }}</el-button>
      </span>
    </el-dialog>

    <!-- 运输线路dialog -->
    <el-dialog
      v-dialogDrag
      :title="tranDialogInfo.title"
      :visible.sync="tranDialogInfo.show"
      width="50%"
      v-el-drag-dialog
      destroy-on-close
      :close-on-click-modal="false"
      top="30vh"
    >
      <el-table :data="tranDialogInfo.table.data" border height="200">
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="起始地" prop="site"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="目的地" prop="destination"></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click=";(tranDialogInfo.show = false), (tranDialogInfo.table.data = [])">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 驳回历史 -->
    <el-dialog
      v-dialogDrag
      :title="HisDialogInfo.title"
      :visible.sync="HisDialogInfo.show"
      width="50%"
      v-el-drag-dialog
      destroy-on-close
      :close-on-click-modal="false"
      top="30vh"
    >
      <el-table :data="HisDialogInfo.table.data" border height="200">
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="驳回信息" prop="sfdoMessage"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="驳回人" prop="sfdoCreator"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="驳回时间" prop="sfdoCreatetime"></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click=";(HisDialogInfo.show = false), (HisDialogInfo.table.data = []), (BoHcount = '')">关闭</el-button>
      </span>
    </el-dialog>

    <!-- 驳回需求dialog -->
    <el-dialog
      v-dialogDrag
      :title="refuseDialog.title"
      :visible.sync="refuseDialog.show"
      width="30%"
      v-el-drag-dialog
      destroy-on-close
      :close-on-click-modal="false"
      top="30vh"
    >
      <el-form :model="refuseFrom.data" :rules="refuseFrom.rules" ref="refuseFromRef">
        <el-form-item prop="refuseMsg">
          <el-row>
            <el-col :span="24">
              <el-input v-model="refuseFrom.data.refuseMsg" type="textarea" placeholder="请输入驳回意见" maxlength="200"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button plain size="mini" type="primary" @click="refuseClose">关闭</el-button>
        <el-button size="mini" type="primary" @click="refuseConfirm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { accAdd } from '@/utils/utils'
import store from '@/store'
import {
  addBid,
  bidDropDown,
  confirmDemand,
  genBidCode,
  genBidName,
  kindType,
  queryDemand,
  queryDemandLine,
  queryHistory,
  refuseDemand,
  sugarType,
  updateBid
} from '@/request/api'
import scrollChange from '@/utils/onscroll'

export default {
  data() {
    return {
      transportTypeCode: '',
      queryChildName: '',
      groupDataList: [], // 集团信息下拉
      bidTypeDataList: [], // 标的类型 运输类型  下拉
      bidStatusDataList: [], //
      bidQuotations: [], // 竞价方式下拉
      transportModeList: [], // 运输方式下拉
      kindModeList: [], // 品种下拉
      productModeList: [], // 产品大类下拉
      sugarModeList: [], // 糖厂下拉
      dialogFrom: {
        // 顶部from表单
        disable: {
          allDisable: false, // 是否全部禁用
          togtherDisable: false
        },
        // 顶部数据表单
        data: {
          bidAmount: 0,
          bidCode: '',
          bidName: '',
          bidQuotations: '',
          bidRange: '',
          bidRemark: '',
          bidPlatformTogether: 'N',
          bidOfferStart: '',
          bidOfferEnd: '',
          bidType: 'ZJZY',
          bidId: ''
        },
        rules: {
          bidCode: [{ required: true, message: '输入标的编号', trigger: 'blur' }],
          bidType: [{ required: true, message: '请选择运输类型', trigger: 'change' }],
          bidName: [{ required: true, message: '请输入标的名称', trigger: 'blur' }],
          bidPlatformTogether: [{ required: true, message: '请选择是否平台合作', trigger: 'blur' }]
        }
      },
      // 运输线路dialog
      tranDialogInfo: {
        title: '',
        show: false,
        table: {
          data: []
        }
      },
      // 驳回历史dialog
      HisDialogInfo: {
        title: '',
        show: false,
        table: {
          data: []
        }
      },
      // 需求驳回dialog
      refuseDialog: {
        show: false,
        title: '',
        currentData: {}
      },
      refuseFrom: {
        data: {
          refuseMsg: ''
        },
        rules: {
          refuseMsg: [{ required: true, message: '请输入驳回意见', trigger: 'blur' }]
        }
      },
      // 生成标的名称
      genName: {
        dptName: '',
        typeName: '',
        code: ''
      },
      // 页面表格
      dTable: {
        data: [],
        loading: false,
        queryParam: {
          code: '',
          transportMode: '',
          tranType: '',
          dptId: '',
          site: '',
          kind: '',
          product: '',
          submitStartTime: '',
          submitEndTime: '',
          group: ''
        },
        selectChangeData: [],
        currentData: {},
        pageInfo: {
          pageIndex: 1,
          pageSize: 10,
          total: 0
        }
      },
      demandSelectList:[] // 需求发布页面携带的数据
    }
  },
  props: {
    info: {
      type: Object,
      required: true
    }
  },
  mounted() {
    scrollChange(document.querySelector('.scrollTable .el-table__body-wrapper'))
  },
  methods: {
    // dialog 关闭回调
    dialogClose() {
      this.demandSelectList = []
      this.transportTypeCode = ''
      this.dTable.pageInfo.pageSize = 10
      this.$refs.dialogFrom.resetFields()
      this.dTable.selectChangeData = []
      this.dTable.data = []
      this.dTable.queryParam = {}
      this.$parent.tableDomain.mTable.currentData = {}
      this.dialogFrom.disable.allDisable = false
      this.dialogFrom.disable.togtherDisable = false
      if (this.info.source == 1) {
        // 糖厂货主
        this.$parent.refreshList()
      } else if (this.info.source == 2) {
        // 集团
        this.$parent.initMTable()
      } else if (this.info.source == 3) {
        // 平台
        this.$parent.initMTable()
      }
    },
    // dialog 显示回调
    dialogShow() {
      if (this.info.source == 3) {
        this.queryChildName = '糖厂/货主:'
      } else {
        this.queryChildName = '糖厂:'
      }
      if (this.info.title == '编辑') {
        this.dialogFrom.data = this.info.currentData
      } else {
        if (this.info.title != '需求确认') {
          this.dialogFrom.data.bidOfferStart = 0.5
          this.dialogFrom.data.bidOfferEnd = 600
          this.dialogFrom.data.bidPlatformTogether = 'Y'
          this.dialogFrom.data.bidAmount = 0
          this.dialogFrom.data.bidName = ''
          this.dialogFrom.data.bidType = 'ZJZY'
          this.genName.typeName = 'ZJZY'
          this.dialogFrom.data.bidQuotations = 'AXJJ'
          this.initBidCodeName()
        }
      }
      if (this.info.source == 3) {
        this.dialogFrom.disable.togtherDisable = true
      }
      // 处理需求发布携带的数据
      if (this.info.source === 1) {
        this.demandSelectList = this.info.selectData
      }
      // 初始化下拉框
      this.initBidType()
      this.initBidStatus()
      this.initBidQuotations()
      this.initTransportModeList()
      this.initKindList()
      this.initProductList()
      this.initSugarList()
      if (this.info.groupShow) {
        this.initGroupDataList()
      }
      // 初始化表格数据
      this.initDemandTable()
    },
    // 初始化表格
    initDemandTable() {
      let data = {
        code: this.dTable.queryParam.code,
        site: this.dTable.queryParam.site,
        destination: this.dTable.queryParam.destination,
        pageIndex: this.dTable.pageInfo.pageIndex,
        pageSize: this.dTable.pageInfo.pageSize,
        transportMode: this.dTable.queryParam.transportMode,
        kind: this.dTable.queryParam.kind,
        product: this.dTable.queryParam.product,
        submitStartTime: this.dTable.queryParam.submitStartTime,
        submitEndTime: this.dTable.queryParam.submitEndTime,
        status: 'YFB',
        tranType: this.dTable.queryParam.tranType,
        source: 1
      }
      // 糖厂
      if (this.info.source == 1) {
        data.source = 0
        data.status = 'NFB'
        data.dptId = store.state.user.DPTID_DX_H
        if (this.demandSelectList.length) {
          data.ids = []
          for (let item of this.demandSelectList) {
            data.ids.push(item.sugarId)
          }
        }
      }
      // 集团
      if (this.info.source == 2) {
        data.source = this.info.title == '需求确认' ? 3 : 1
        data.status = 'YFB'
        data.dptId = this.dTable.queryParam.dptId
        data.sugarDptId = store.state.user.DPTID_DX_H
      }
      // 平台
      if (this.info.source == 3) {
        data.status = 'JTSH'
        data.source = 2
        data.dptId = this.dTable.queryParam.dptId
        data.sugarDptId = this.dTable.queryParam.group
      }
      if (this.info.title == '编辑') {
        data.checked = '1'
        data.bidId = this.dialogFrom.data.bidId
      }
      this.transportTypeCode = ''
      queryDemand(data).then(res => {
        // 勾选糖厂携带的数据
        if (this.demandSelectList.length) {
          for (let re of res.data) {
            if (data.ids.includes(re.sugarId)) {
              this.dTable.selectChangeData.push(re)
            }
          }
        }
        if (this.dTable.selectChangeData.length === 0) {
          // 之前没有勾选
          this.dTable.data = res.data
          this.dTable.pageInfo.total = res.total
          if (this.info.title == '编辑' || this.info.title == '需求确认') {
            setTimeout(() => {
              for (let i in this.$refs.dTableRef.data) {
                if (this.info.title == '编辑' && this.$refs.dTableRef.data[i].checked == 'Y') {
                  this.$refs['dTableRef'].toggleRowSelection(this.$refs.dTableRef.data[i], true)
                }
                if (this.info.title == '需求确认' && this.$refs.dTableRef.data[i].status == 'JTSH') {
                  this.$refs.dTableRef.toggleRowSelection(this.$refs.dTableRef.data[i], true)
                }
              }
            }, 200)
          }
        } else {
          // 之前被勾选了
          const allData = new Map()
          const allSelect = this.dTable.selectChangeData
          if (this.info.title == '编辑' || this.info.title == '需求确认') {
            allSelect.concat(
              res.data.filter(r => {
                if (this.info.title == '编辑') {
                  return r.checked == 'Y'
                }
                if (this.info.title == '需求确认') {
                  return r.status == 'JTSH'
                }
                return false
              })
            )
          }
          allSelect
            .sort((a, b) => {
              return parseInt(b.demandCode) - parseInt(a.demandCode)
            })
            .forEach(a => allData.set(a.sugarId, a))
          res.data.forEach(r => allData.set(r.sugarId, r))
          this.dTable.data = []
          allData.forEach((v, k) => this.dTable.data.push(v))
          this.dTable.selectChangeData = []
          this.$refs.dTableRef.clearSelection()
          setTimeout(() => {
            allSelect.forEach(a =>
              this.dTable.data.forEach(b => {
                if (a.sugarId == b.sugarId) {
                  this.$refs.dTableRef.toggleRowSelection(b, true)
                }
              })
            )
          }, 200)
        }
      })
    },
    // 标的类型下拉框
    initBidType() {
      bidDropDown({
        name: '标的类型'
      }).then(res => {
        if (res.success == 1) {
          this.bidTypeDataList = res.data
        }
      })
    },
    // 标书状态下拉
    initBidStatus() {
      bidDropDown({
        name: '标书状态'
      }).then(res => {
        if (res.success == 1) {
          this.bidStatusDataList = res.data
        }
      })
    },
    // 标书竞价方式
    initBidQuotations() {
      bidDropDown({
        name: '标书竞价方式'
      }).then(res => {
        if (res.success == 1) {
          this.bidQuotations = res.data
        }
      })
    },
    // 运输方式下拉
    initTransportModeList() {
      bidDropDown({
        name: '运输类型'
      }).then(res => {
        this.transportModeList = res.data
      })
    },
    // 品种下拉
    initKindList() {
      let data = {}
      // 糖厂
      if (this.info.source == 1) {
        data.dptId = store.state.user.DPTID_DX_H
      }
      // 集团
      if (this.info.source == 2) {
        data.preDptId = store.state.user.DPTID_DX_H
      }
      // 平台
      if (this.info.source == 3) {
        data.isp = 1
      }
      kindType(data).then(res => {
        if (res.success == 1) {
          this.kindModeList = res.data
        }
      })
    },
    // 产品大类下拉
    initProductList() {
      bidDropDown({
        name: '产品大类'
      }).then(res => {
        if (res.success == 1) {
          this.productModeList = res.data
        }
      })
    },
    // 糖厂下拉
    initSugarList() {
      sugarType({
        dptPredptId: this.info.source == 3 ? '' : store.state.user.DPTID_DX_H
      }).then(res => {
        this.sugarModeList = res.data
      })
    },
    // 集团下拉
    initGroupDataList() {
      sugarType({
        dptType: 'JT'
      }).then(res => {
        this.groupDataList = res.data
      })
    },
    // 获取标的代码和名称
    initBidCodeName() {
      genBidCode().then(res => {
        if (res.success == 1) {
          this.dialogFrom.data.bidCode = res.msg
          this.genName.code = res.msg
          genBidName().then(res => {
            this.genName.dptName = res.msg ? res.msg : ''
            this.dialogFrom.data.bidName = (this.genName.dptName ? this.genName.dptName : '') + this.dialogFrom.data.bidType + this.genName.code
          })
        }
      })
    },
    // 初始化线路详情
    initTranMethodTable() {
      queryDemandLine({
        sugarId: this.dTable.currentData.sugarId
      }).then(res => {
        if (res.success == 1) {
          this.tranDialogInfo.table.data = res.data
        }
      })
    },
    // 初始化历史驳回记录
    initHisMethodTable() {
      queryHistory({
        demandCode: this.dTable.currentData.demandCode
      }).then(res => {
        if (res.success == 1) {
          this.HisDialogInfo.table.data = res.data.list
          this.BoHcount = res.data.number
        }
      })
    },
    // 确认
    confirm() {
      this.dialogFrom.data.bidType = this.transportTypeCode
      if (this.info.title == '汇总需求') {
        this.$refs['dialogFrom'].validate(valid => {
          if (valid) {
            if (this.dTable.selectChangeData.length == 0) {
              this.$message.error('请选择需求')
              return
            }
            addBid({
              from: Object.assign(this.dialogFrom.data, {
                source: this.info.groupShow ? 3 : 2,
                bidStatus: this.info.groupShow ? 'PTHZ' : ''
              }),
              demandDTOS: this.dTable.selectChangeData
            }).then(res => {
              if (res.msg === '2002') {
                this.$confirm('标的编号重复，点击确认以刷新编号', '提示', {
                  confirmButtonText: '确定',
                  showCancelButton: false
                }).then(() => {
                  genBidCode().then(res => {
                    this.dialogFrom.data.bidCode = res.msg
                  })
                })
                return
              }
              if (res.success == 1) {
                this.info.show = false
                this.$message.success('汇总成功')
              }
            })
            return
          }
          this.$message.error('请输入必填项')
        })
      } else if (this.info.title == '编辑') {
        if (this.dTable.selectChangeData.length == 0) {
          this.$message.error('请至少选择一条需求')
          return
        }
        let p = {}
        Object.assign(p, this.dialogFrom.data)
        p.demandDTOS = this.dTable.selectChangeData
        p.bidStatus = ''
        if (this.info.groupShow) {
          p.source = 3
        }
        updateBid(p).then(res => {
          if (res.success == 1) {
            this.$message.success('修改成功')
            this.info.show = false
          }
        })
      } else if (this.info.title == '提交平台') {
        let f = this.dialogFrom.data
        const type = localStorage.getItem('IS_SHOW1')
        if (type == 6) {
          f.bidStatus = 'TCTJ'
        }
        if (type == 3) {
          f.bidStatus = 'HZTJ'
        }
        // 表明是从糖厂 货主直接提交的
        f.source = 1
        addBid({
          from: f,
          demandDTOS: this.dTable.selectChangeData
        }).then(res => {
          if (res.msg === '2002') {
            this.$confirm('标的编号重复，点击确认以刷新编号', '提示', {
              confirmButtonText: '确定',
              showCancelButton: false
            }).then(() => {
              genBidCode().then(res => {
                this.dialogFrom.data.bidCode = res.msg
              })
            })
            return
          }
          if (res.success == 1) {
            this.info.show = false
            this.$message.success('提交成功')
          }
        })
      } else if (this.info.title == '需求确认') {
        if (this.dTable.selectChangeData.length == 0) {
          this.$message.error('至少选择一需求')
          return
        }
        let data = []
        this.dTable.selectChangeData.forEach(d => {
          if (d.status == 'YFB') {
            data.push(d)
          }
        })
        confirmDemand({
          list: data
        }).then(res => {
          this.$message.success('确认成功')
          this.info.show = false
        })
      } else {
        this.$message.error('无效的选项')
        this.info.show = false
      }
    },
    // 驳回确认
    refuseConfirm() {
      this.$refs['refuseFromRef'].validate(valid => {
        if (valid) {
          refuseDemand({
            demandCode: this.refuseDialog.currentData.demandCode,
            sfdRefuseMsg: this.refuseFrom.data.refuseMsg,
            status: 'BOH',
            sugarId: this.refuseDialog.currentData.sugarId
          }).then(res => {
            this.$message.success('驳回成功')
            this.initDemandTable()
            this.refuseClose()
          })
        }
      })
    },
    // 驳回取消
    refuseClose() {
      if (this.dTable.selectChangeData.length != 0) {
        for (let i in this.dTable.selectChangeData) {
          if (this.dTable.selectChangeData[i].demandCode == this.refuseDialog.currentData.demandCode) {
            this.dTable.selectChangeData.splice(i, 1)
            break
          }
        }
        this.demandSelectChange(this.dTable.selectChangeData)
      }
      this.refuseDialog.currentData = {}
      this.refuseFrom.data = {}
      this.$refs['refuseFromRef'].resetFields()
      this.refuseDialog.show = false
    },
    dialogInputNumberStartChange(val) {
      if (val > this.dialogFrom.data.bidOfferEnd) {
        this.dialogFrom.data.bidOfferEnd = val
      }
    },
    dialogInputNumberEndChange(val) {
      if (val < this.dialogFrom.data.bidOfferStart) {
        this.dialogFrom.data.bidOfferStart = val
      }
    },
    bidTypeChange(val) {
      this.genName.typeName = val
      this.dialogFrom.data.bidName = (this.genName.dptName ? this.genName.dptName : '') + val + this.genName.code
    },
    // 驳回需求按钮
    refuse(val) {
      this.refuseDialog.currentData = val
      this.refuseDialog.title = '驳回'
      this.refuseDialog.show = true
    },
    // dialog 查看线路详细信息
    showTransportMethod(val) {
      this.dTable.currentData = val
      this.initTranMethodTable()
      this.tranDialogInfo.title = '线路详情'
      this.tranDialogInfo.show = true
    },
    // dialog 查看历史记录
    showHistory(val) {
      this.dTable.currentData = val
      this.initHisMethodTable()
      this.HisDialogInfo.title = '驳回历史'
      this.HisDialogInfo.show = true
    },
    demandSelectChange(val) {
      if (val.length === 0) {
        this.transportTypeCode = ''
        this.dialogFrom.data.bidAmount = 0
        this.dTable.selectChangeData = []
        return
      }
      this.dTable.selectChangeData = []
      this.transportTypeCode = this.transportTypeCode || val[0].transportTypeCode
      val.forEach(v => {
        this.dTable.selectChangeData.push(v)
        if (v.transportTypeCode !== this.transportTypeCode) {
          this.$message.error({
            message: '勾选的数据中运输类型不一致,无效的勾选项',
            showClose: true
          })
          this.$nextTick(() => {
            this.$refs.dTableRef.clearSelection()
            this.dTable.selectChangeData = []
            this.transportTypeCode = ''
          })
        }
      })
      this.dialogFrom.data.bidAmount = 0
      let sum = 0
      val.forEach(e => {
        sum = accAdd(sum, parseFloat(e.transportQuantity))
      })
      this.dialogFrom.data.bidAmount = sum
      console.log('选择', this.dTable.selectChangeData)
    },
    dci(i) {
      this.dTable.pageInfo.pageIndex = i
      this.initDemandTable()
    },
    dcs(i) {
      this.dTable.pageInfo.pageSize = i
      this.initDemandTable()
    },
    isSelectable(row, i) {
      if (this.info.title == '需求确认') {
        return row.status != 'JTSH'
      }
      if (this.transportTypeCode) {
        return row.transportTypeCode == this.transportTypeCode
      }
      return true
    },
    dTableRowKey(row) {
      return row.sugarId
    },
    dialogOpened() {
      this.$nextTick(() => {
        scrollChange(document.querySelector('#scrollTable .el-table__body-wrapper'))
      })
    },
    rowClick(row) {
      this.$refs.dTableRef.toggleRowSelection(row)
    }
  }
}
</script>
<style lang="scss" scoped>
.basicInfoBox {
  border: 1px solid #cccccc;
  border-radius: 5px;
  padding-top: 10px;
}
::v-deep .el-form-item__content {
  margin-left: 10px !important;
}

.tableTitle {
  font-size: 15px;
  margin-top: 10px;
  padding-top: 10px;
  padding-bottom: 10px;
  font-weight: bold;
}
</style>
